# -*- coding: utf-8 -*-
# @Author: Turbidsoul
# @Date:   2017-05-22 22:48:18
# @Last Modified by:   Turbidsoul
# @Last Modified time: 2017-06-01 00:33:33

from dida365.api import DidaV2
from ptest import config
from ptest.assertion import assert_not_none, assert_true
from ptest.decorator import AfterClass, BeforeClass, Test, TestClass
from ptest.plogger import preporter

@TestClass(run_mode='singleline')
class DidaV2Test(object):
  
  @BeforeClass(description='Prepare test data')
  def before(self):
    preporter.info("setting dida365 username and password")
    self.expected = 10
    self.username = config.get_property('u')
    self.password = config.get_property('p')
    self.v2 = DidaV2()
    self.v2.signon(self.username, self.password)

  @Test(description="参数测试")
  def config_test(self):
    preporter.info("username:" + self.username)
    preporter.info("password:" + self.password)
    assert_not_none(self.username, 'username is None')
    assert_not_none(self.password, 'password is None')

  @Test(timeout=10, description="登陆测试", group='api')
  def signon_test(self):
    assert_not_none(self.v2.token, 'token is None')
    preporter.info("token:" + self.v2.token)
    assert_not_none(self.v2.account.userId, 'userId is None')
    preporter.info('userId:' + self.v2.account.userId)
    assert_true(self.v2.account.username == self.username, 'username is fail')
    preporter.info('username:' + self.v2.account.username)
    preporter.info('json:' + str(self.v2.account.json))

  @Test(timeout=10, description='project 测试', group='api')
  def project_test(self):
    projects = self.v2.fetch_projects()
    assert_true(len(projects) > 0, 'has projects')
    preporter.info('has {} projects'.format(len(projects)));

  @Test(timeout=10, description='Calendar 测试', group='api')
  def calendar_test(self):
    bindevents = self.v2.fetch_bind_events()
    assert_true(len(bindevents) > 0, 'Calendar bind event great then 0')
    preporter.info('has {} bind events'.format(len(bindevents)))

  @Test(timeout=10, description='Setting 测试', group='api')
  def setting_test(self):
    pluginsettings = self.v2.fetch_pluginsettings()
    assert_true(len(pluginsettings) > 0, 'plugin settings great then 0')
    preporter.info('has {} plugin settings'.format(len(pluginsettings)))

  @AfterClass(always_run=True, description='Clean up')
  def after(self):
    preporter.info('cleaning up')
